/**
 * Created by jimmy on 2017/6/11.
 */

var mysqlConn = require("./BaseDao");

exports.insPreOrd = function (order,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("insert into wh_order set ?",order,function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.getOrder = function (usrId,status,callbk) {
    mysqlConn.getConn(function (err,connection) {
        var sql = "select a.ord_id,a.prod_id,a.spec_id,a.prod_sum,a.prod_price,a.user_id,b.product_price," +
            "b.product_name,b.product_icon,product_desc,c.value from wh_order a,product b,product_detail c " +
            "where a.prod_id = b.product_id and a.spec_id = c.detail_id and a.user_id = ?";
        if(status != 0){
            sql += " and a.status = "+status
        }
        connection.query(sql,[usrId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getOrderSendFee = function (usrId,status,callbk) {
    mysqlConn.getConn(function (err,connection) {
        var sql = "select sum(b.`value`) as send_fee from wh_order a,product_detail b where a.prod_id = b.product_id and b.type = 'sendfee' and a.user_id = ?";
        if(status != 0){
            sql += " and a.status = "+status
        }
        connection.query(sql,[usrId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.delOrder = function (ordId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("update wh_order set status = 9 where ord_id = ?",[ordId],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.addProdSum  = function (ordId,sum,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("update wh_order set prod_sum = prod_sum + ? where ord_id = ?",[sum,ordId],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.subOrd = function (ordIds,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("update wh_order set status = 2 where ord_id in ("+ordIds+")",[],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.paySucOrd = function (ordIds,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("update wh_order set status = 3 where ord_id in ("+ordIds+")",[],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.cancelOrd = function (ordIds,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("update wh_order set status = 1 where ord_id in ("+ordIds+")",[],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.getAddr = function (userId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("select * from user_addr where user_id = ?",[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.delAddr = function (addrId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("delete from user_addr where addr_id = ?",[addrId],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.addAddr = function (addInfo,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("insert into user_addr set ?",addInfo,function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.setdef = function (addrid,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("update user_addr set isdefault = 1 where addr_id = ?",[addrid],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.setnodef = function (addrid,userid,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("update user_addr set isdefault = 0 where addr_id <> ? and user_id = ?",[addrid,userid],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.insWxNotify = function (param,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("insert into wx_notify set ?",param,function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.getOrdSum = function (userId,callbk) {
    var sql = 'select count(ord_id) as cnt, `status` from wh_order where user_id = ? group by `status`';
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getOrdSendFee = function (userId,callbk) {
    var sql = "select sum(`value`) as sendfee from wh_order a,product_detail b where a.prod_id = b.product_id and b.type = 'sendfee' and a.user_id = ? and a.`status` = 2";
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.getPayOrd = function (str,userId,callbk) {
    var sql = "select out_trade_no,cash_fee from wx_notify where nonce_str = ? and openid = ?";
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[str,userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.insOrdAddr = function (payOrdId,addrId,callbk) {
    mysqlConn.getConn(function (err,connection) {
        connection.query("insert into ord_addr(addr_id,pay_order_id) values(?,?)",[addrId,payOrdId],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}